import is.glif.fenius.ws.simple.ReservationParams;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.datatype.DatatypeFactory;

import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FeniusCreate extends HttpServlet {
    public static final long serialVersionUID = 01010102L;
    FeniusClient client = null;

    static {
	TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    }

    public FeniusCreate() 
    {
	client = new FeniusClient();
    }

    public static synchronized String getString(Calendar c) {
	if (c == null) return null;

	String s = c.get(Calendar.YEAR)
	    + "/" + (c.get(Calendar.MONTH)+1)
	    + "/" + c.get(Calendar.DAY_OF_MONTH)
	    + " " + c.get(Calendar.HOUR_OF_DAY)
	    + ":" + c.get(Calendar.MINUTE)
	    + ":" + c.get(Calendar.SECOND);
	return s;
    }

    public static Calendar getCalendar()
    {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(new Date());
	calendar.set(Calendar.MILLISECOND, 0);
	return calendar;
    }

    private String head1 =
	"<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><title>Fenius/GNS-WSIv3 Reservation</title><body>";
    private String logo = 
	"<table width=\"96%\" align=center><TR><TD><img alt=\"G-lambda logo\" src=\"http://www.g-lambda.net/wordpress/wp-content/uploads/2006/10/the_g-lambda_logo_s.jpg\"></TD><TD><H1>Fenius/GNS-WSIv3 Reservation Creator</H1></TD></TABLE>\n";

    private String tail = "</body></html>\n";
    static int no = 1;
    String ap = "IXA1";
    String zp = "IXA2";
    Integer duration = new Integer(60);
    Integer bw = new Integer(100);

    ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
	System.err.println("=========== doGet create start now\n");
	String s = head1 + logo;
	// s += "<H1 align=center>Fenius/G-lambda Reservation Creator</H1>\n";

	s += "<table width=\"96%\" align=\"center\" border=\"1\" cellpadding=\"5\">\n";
        s += "<form action=\"FeniusCreate\" method=POST>";

	s += "<tr bgcolor=\"#C0C0C0\"><th>Reservation ID</th><th>Terminal<BR>Point</th><th>Terminal<BR>Point</th><th>Activate<BR>Time</th><th>Duration Time(sec)</th><th>BW (Mbps)</th><th>SUBMIT</th></tr>\n";

	ReservationParams resvParams = null;
	String status = "Submitted";
	String rid = null;
	try {
	    String qid = request.getParameter("rid");
	    String qap = request.getParameter("src");
	    String qzp = request.getParameter("dst");
	    String qtime = request.getParameter("time");
	    String qduration = request.getParameter("duration");
	    String qbw = request.getParameter("bw");

	    rid = qid;
	    if (rid != null) {
		s += "<td>" + qid + "</td>";
		s += "<td>" + qap + "</td>";
		s += "<td>" + qzp + "</td>";
		s += "<td>" + qtime + "</td>";
		s += "<td>" + qduration + "</td>";
		s += "<td>" + qbw + "</td>";
	    }

	    Calendar stime = null;
	    if (qid != null && qap != null && qzp != null && 
		qtime != null && qduration != null && qbw != null) {
		
		rid = qid;
		ap = qap;
		zp = qzp;
		duration  = new Integer(qduration);
		bw = new Integer(qbw);
		
		String[] item = qtime.split("[:,.;/ ]+");
		if (item.length > 5) {
		    stime = getCalendar();
		    stime.set(Calendar.YEAR, Integer.parseInt(item[0]));
		    stime.set(Calendar.MONTH, Integer.parseInt(item[1]) -1);
		    stime.set(Calendar.DATE, Integer.parseInt(item[2]));
		    stime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(item[3]));
		    stime.set(Calendar.MINUTE, Integer.parseInt(item[4]));
		    stime.set(Calendar.SECOND, Integer.parseInt(item[5]));
		}
	    }

	    if (stime != null) {
		client.doCreate(rid, stime, duration, bw * 1000000, ap, zp);
	    } else {
		status = "Input Error";
	    }
	} catch (Exception ex) {
	    status = "Remote Error";
	    ex.printStackTrace();
	}

	if (rid != null) {
	    s += "<td>" + status + "</td>";
	    s += "</tr>\n";
	}

		
	rid = "Glambda-" + no++;
	Calendar c = getCalendar();
	c.add(Calendar.MINUTE, 2);

	s += "<tr>";
	s += "<td> <input type=text name=rid value=\"" + rid + "\"/> </td>";
	s += "<td> <input type=text name=src size=6 value=\"" + ap + "\"/> </td>";
	s += "<td> <input type=text name=dst size=6 value=\"" + zp + "\"/> </td>";
	s += "<td> <input type=text name=time value=\"" + 
	    getString(c) + "\"/> </td>";
	s += "<td> <input type=text name=duration size=6 value=\"" + duration + "\"/> </td>";
	s += "<td> <input type=text name=bw size=6 value=\"" + bw + "\"/> </td>";
	s += "<td> <input type=submit value=CREATE /> </td>";

	s += "</tr>\n";
	s += "</form></table>\n";
	s += tail;

	response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println(s);
	System.err.println("=========== doGet create end \n");
    }

    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        doGet(request, response);
    }
}
